local quzong = fk.CreateSkill{
  name = "hanqing__quzong",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["hanqing__quzong"] = "驱纵",
  [":hanqing__quzong"] = "限定技，轮次开始时，你可以选择一名角色，本轮当其成为三次不同使用者的牌目标后，其需令你获得其两张牌或翻面。",

  ["#hanqing__quzong-choose"] = "驱纵：选择一名角色，本轮当其成为三次不同使用者的牌目标后，你获得其两张牌或其翻面！",
  ["@hanqing__quzong-round"] = "驱纵",
  ["#hanqing__quzong-invoke"] = "驱纵：令 %src 获得你两张牌，或点“取消”你翻面",
  ["#hanqing__quzong-prey"] = "驱纵：获得 %dest 两张牌",
}

quzong:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(quzong.name) and player:usedSkillTimes(quzong.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room.alive_players,
      skill_name = quzong.name,
      prompt = "#hanqing__quzong-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(player, "hanqing__quzong-round", to.id)
    room:setPlayerMark(to, "@hanqing__quzong-round", 3)
  end,
})

quzong:addEffect(fk.TargetConfirmed, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("hanqing__quzong-round") == target.id and
      data.extra_data and data.extra_data.hanqing__quzong and not target.dead
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "hanqing__quzong-round", 0)
    if #target:getCardIds("he") < 2 then
      target:turnOver()
    else
      if room:askToSkillInvoke(target, {
        skill_name = quzong.name,
        prompt = "#hanqing__quzong-invoke:"..player.id,
      }) then
        local cards = room:askToChooseCards(player, {
          target = target,
          min = 2,
          max = 2,
          flag = "he",
          skill_name = quzong.name,
          prompt = "#hanqing__quzong-prey::"..target.id,
        })
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, quzong.name, nil, false, player)
      else
        target:turnOver()
      end
    end
  end,

  can_refresh = function(self, event, target, player, data)
    return player:getMark("hanqing__quzong-round") == target.id
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("hanqing__quzong_record-round")
    if table.insertIfNeed(mark, data.from.id) then
      room:setPlayerMark(player, "hanqing__quzong_record-round", mark)
      room:removePlayerMark(target, "@hanqing__quzong-round", 1)
      if #mark > 2 then
        data.extra_data = data.extra_data or {}
        data.extra_data.hanqing__quzong = true
        room:setPlayerMark(player, "hanqing__quzong_record-round", 0)
        room:setPlayerMark(target, "@hanqing__quzong-round", 0)
      end
    end
  end,
})

return quzong
